package org.jeecg.modules.dlxj.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.dlxj.entity.DlxjDebugStatistics;
import org.jeecg.modules.dlxj.entity.DlxjPatrolStatistics;
import org.jeecg.modules.dlxj.mapper.DlxjPatrolStatisticsMapper;
import org.jeecg.modules.dlxj.service.IDlxjPatrolStatisticsService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 巡检记录统计
 * @Author: lzl
 * @Date:   2022-07-29
 * @Version: V1.0
 */
@Service
public class DlxjPatrolStatisticsServiceImpl extends ServiceImpl<DlxjPatrolStatisticsMapper, DlxjPatrolStatistics> implements IDlxjPatrolStatisticsService {

    @Autowired
    private DlxjPatrolStatisticsMapper dlxjPatrolStatisticsMapper;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;

    /**
     * 巡检统计列表
     * @return
     */
    @Override
    public List<DlxjPatrolStatistics> patrolStatisticsList() {
        return dlxjPatrolStatisticsMapper.patrolStatisticsList();
    }

    /**
     * 巡检统计，通过任务id和杆塔id查询
     * @param taskId
     * @param poleId
     * @return
     */
    @Override
    public List<DlxjPatrolStatistics> queryByTaskIdAndPoleId(String taskId,String poleId) {
        return dlxjPatrolStatisticsMapper.queryByTaskIdAndPoleId(taskId,poleId);
    }

    /**
     * 导出全部数据
     * @param request
     * @param dlxjPatrolStatistics
     * @param clazz
     * @param title
     * @param taskId
     * @param poleId
     * @return
     */
    @Override
    public ModelAndView exportExcel(HttpServletRequest request, DlxjPatrolStatistics dlxjPatrolStatistics, Class<DlxjPatrolStatistics> clazz, String title,String taskId,String poleId) {
        // Step.1 组装查询条件
        //QueryWrapper<DlxjPatrolStatistics> queryWrapper = QueryGenerator.initQueryWrapper(dlxjDebugStatistics, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();


        // Step.2 获取导出数据
        List<DlxjPatrolStatistics> pageList;
        if (taskId!=null || poleId!=null){
            pageList = dlxjPatrolStatisticsMapper.queryByTaskIdAndPoleId(taskId,poleId);}
        else {
             pageList = dlxjPatrolStatisticsMapper.patrolStatisticsList();
        }

        List<DlxjPatrolStatistics> exportList = null;

        //过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectionList.contains(sysUser.getId())).collect(Collectors.toList());
        } else {
            exportList = pageList;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, clazz);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams=new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
        exportParams.setImageBasePath(upLoadPath);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS,exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }
}
